home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFtoSpr / c / PreQuit < prev    next >
Encoding:
Text File  |  2003-11-06  |  5.8 KB  |  191 lines

  1. /*
  2.  *  SFtoSpr - Star Fighter 3000 graphics converter
  3.  *  Quit confirm dbox
  4.  *  Copyright (C) 2000  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25.  
  26. /* RISC OS library files */
  27. #include "event.h"
  28. #include "toolbox.h"
  29. #include "quit.h"
  30. #include "wimplib.h"
  31. #include "wimp.h"
  32.  
  33. /* My library files */
  34. #include "err.h"
  35. #include "msgtrans.h"
  36. #include "Macros.h"
  37. #include "RoundRobin.h"
  38. #include "TboxBugs.h"
  39. #ifdef FOCUS_CRAP
  40. #include "InputFocus.h"
  41. #endif
  42. #include "ViewsMenu.h"
  43.  
  44. /* Local headers */
  45. #include "PreQuit.h"
  46. #include "Main.h"
  47.  
  48. static ObjectId dbox_id = NULL_ObjectId;
  49. static int quit_sender;
  50. #ifdef HELP_CRAP
  51. static int window_handle;
  52. #endif
  53.  
  54. /* ----------------------------------------------------------------------- */
  55. /*                       Function prototypes                               */
  56.  
  57. static ToolboxEventHandler _PreQuit_event_handler;
  58. #ifdef HELP_CRAP
  59. static WimpMessageHandler _PreQuit_menus_deleted;
  60. #endif
  61.  
  62. /* ----------------------------------------------------------------------- */
  63. /*                         Public functions                                */
  64.  
  65. void PreQuit_initialise(ObjectId PreQuit_id)
  66. {
  67.   /* Record ID */
  68.   dbox_id = PreQuit_id;
  69.  
  70.   /* Install handlers */
  71. #ifdef FOCUS_CRAP
  72.   EF(event_register_toolbox_handler(PreQuit_id, Quit_AboutToBeShown, InputFocus_recordcaretpos, NULL));
  73. #endif
  74.   EF(event_register_toolbox_handler(PreQuit_id, -1, _PreQuit_event_handler, NULL)); /* all events */
  75.   
  76. #ifdef HELP_CRAP
  77.   ObjectId window_id;
  78.   EF(quit_get_window_id(0, PreQuit_id, &window_id));
  79.   EF(window_get_wimp_handle(0, window_id, &window_handle));
  80.   EF(event_register_message_handler(Wimp_MMenusDeleted, _PreQuit_menus_deleted, NULL));
  81.   /* (See TboxBugs.h for explanation of this. If undefining HELP_CRAP,
  82.   don't forget to set object template to generate Quit_DialogueCompleted
  83.   event) */
  84. #endif
  85. }
  86.  
  87. /* ----------------------------------------------------------------------- */
  88.  
  89. bool TRY_QUIT(int task_handle)
  90. {
  91.   ObjectId object;
  92.   int scanprogress_count = 0;
  93.   char buffer[16];
  94.  
  95.   object = ViewMenu_getfirst();
  96.   while(object != NULL_ObjectId) {
  97.     /* Don't report error if object doesn't exist */
  98.     if(toolbox_get_template_name(0, object, buffer, sizeof(buffer), NULL) == NULL) {
  99.       if(strcmp(buffer, "Scan") == 0)
  100.         scanprogress_count++;
  101.     }
  102.     object = ViewMenu_getnext(object);
  103.   }
  104.   if(scanprogress_count > 1) {
  105.     sprintf(buffer, "%d", scanprogress_count);
  106.     RE(quit_set_message(0, dbox_id, msgs_lookup_sub1("PlurUNS", buffer)))
  107.   }
  108.   else {
  109.     if(scanprogress_count == 1)
  110.       RE(quit_set_message(0, dbox_id, msgs_lookup("SingUNS")))
  111.     else
  112.       return true; /* Don't mind quitting */
  113.   }
  114.  
  115.   quit_sender = task_handle;
  116.   RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, dbox_id, Toolbox_ShowObject_Centre, NULL, NULL_ObjectId, NULL_ComponentId))
  117.   return false; /* Don't want to quit */
  118. }
  119.  
  120. /* ----------------------------------------------------------------------- */
  121. /*                         Private functions                               */
  122.  
  123. static int _PreQuit_event_handler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  124. {
  125.   switch(event_code) {
  126.     case Quit_AboutToBeShown:
  127.       RoundRobin_suspend(); /* freeze all directory scans */
  128. #ifdef FOCUS_CRAP
  129.       return 0;  /* pass event on (to InputFocus_recordcaretpos) */
  130. #else
  131.       return 1; /* claim event */
  132. #endif
  133.  
  134.     case Quit_Quit:
  135. #ifdef FOCUS_CRAP
  136.       /* We won't be alive to hear the MenusDeleted msg, so fake it */
  137.       RE(InputFocus_restorecaret())
  138. #endif
  139.       if(quit_sender != 0) {
  140.         /* Restart desktop shutdown */
  141.         WimpKeyPressedEvent key_event;
  142.         RE(wimp_get_caret_position(&key_event.caret))
  143.         key_event.key_code = 0x1FC;
  144.         RE(wimp_send_message(Wimp_EKeyPressed, &key_event, quit_sender, 0, NULL))
  145.  
  146. #ifdef QUIT_ON_SHUTDOWN
  147.         /* Quit immediately */
  148.         exit(EXIT_SUCCESS);
  149. #else
  150.         /* Do as Paint, Edit and Draw do - that is, discard all data */
  151.         ObjectId object = ViewMenu_getfirst();
  152.         while(object != NULL_ObjectId) {
  153.           /* Don't report error if object doesn't exist */
  154.           char buffer[16];
  155.           if(toolbox_get_template_name(0, object, buffer, sizeof(buffer), NULL) == NULL) {
  156.             if(strcmp(buffer, "Scan") == 0)
  157.               RE(toolbox_delete_object(0, object))
  158.           }
  159.           object = ViewMenu_getnext(object);
  160.         }
  161. #endif
  162.  
  163.       } else {
  164.         /* Quit application */
  165.         exit(EXIT_SUCCESS);
  166.       }
  167.       return 1; /* claim event */
  168.  
  169. #ifndef HELP_CRAP
  170.     case Quit_DialogueCompleted:
  171.       /* Resume all directory scans */
  172.       RE(RoundRobin_resume())
  173.       return 1; /* claim event */
  174. #endif
  175.   }
  176.   return 0; /* pass event on */
  177. }
  178.  
  179. /* ----------------------------------------------------------------------- */
  180.  
  181. #ifdef HELP_CRAP
  182. static int _PreQuit_menus_deleted(WimpMessage *message, void *handle)
  183. {
  184.   /* 'Menu tree' has been closed - is menu block our wimp window? */
  185.   if(message->data.words[0] == window_handle)
  186.     RoundRobin_resume(); /* yes - resume all directory scans */
  187.  
  188.   return 0; /* pass the event on to other handlers */
  189. }
  190. #endif
  191.